package agentService

import (
	"errors"
	"github.com/spf13/cast"
	"hc_server/routers/models"
)

// Rate 费率
type Rate struct {
	RateId              int     //费率主键id
	SettlementId        int     //结算主键id
	AgentId             int     //代理商id
	SuperiorId          int     //上级代理商id
	RateType            int     //费率类型 0=D0 1=D1
	WechatMin           float64 //微信费率最小值
	WechatMax           float64 //微信费率最大值
	AlipayMin           float64 //支付宝费率最小值
	AlipayMax           float64 //支付宝费率最大值
	YunPayLowMin        float64 //云闪付费率<=1000元 最小值
	YunPayLowMax        float64 //云闪付费率<=1000元 最大值
	YunPayHighMin       float64 //云闪付费率>1000元 最小值
	YunPayHighMax       float64 //云闪付费率>1000元 最大值
	DebitCardMin        float64 //刷卡储蓄卡费率 最小值
	DebitCardMax        float64 //刷卡储蓄卡费率 最大值
	DebitCardCappingMin float64 //刷卡储蓄卡封顶手续费 最小值
	DebitCardCappingMax float64 //刷卡储蓄卡封顶手续费 最大值
	CreditCardMin       float64 //刷卡信用卡费率 最小值
	CreditCardMax       float64 //刷卡信用卡费率 最大值
	D0SettlementRateMin float64 //D0付款结算分润百分比 最小值
	D0SettlementRateMax float64 //D0付款结算分润百分比 最大值
	D0SettlementMin     float64 //D0付款结算分润手续费 最小值
	D0SettlementMax     float64 //D0付款结算分润手续费 最大值
	D1SettlementRateMin float64 //D1付款结算分润百分比 最小值
	D1SettlementRateMax float64 //D1付款结算分润百分比 最大值
	D1SettlementMin     float64 //D1付款结算分润手续费 最小值
	D1SettlementMax     float64 //D1付款结算分润手续费 最大值
	WithdrawalRateMin   float64 //提现费率百分比 最小值
	WithdrawalRateMax   float64 //提现费率百分比 最大值
	WithdrawalMin       float64 //提现手续费 最小值
	WithdrawalMax       float64 //提现手续费 最大值
}

// AgentRateAddRateInfo 添加费率信息
func (r *Rate) AgentRateAddRateInfo() error {
	return models.AgentRateAddRateInfo(models.AgentRate{
		AgentId:             r.AgentId,
		RateType:            r.RateType,
		WechatMin:           r.WechatMin,
		WechatMax:           r.WechatMax,
		AlipayMin:           r.AlipayMin,
		AlipayMax:           r.AlipayMax,
		YunPayLowMin:        r.YunPayLowMin,
		YunPayLowMax:        r.YunPayLowMax,
		YunPayHighMin:       r.YunPayHighMin,
		YunPayHighMax:       r.YunPayHighMax,
		DebitCardMin:        r.DebitCardMin,
		DebitCardMax:        r.DebitCardMax,
		DebitCardCappingMin: r.DebitCardCappingMin,
		DebitCardCappingMax: r.DebitCardCappingMax,
		CreditCardMin:       r.CreditCardMin,
		CreditCardMax:       r.CreditCardMax,
	})
}

// AgentRateUpdateRateInfo 修改费率信息
func (r *Rate) AgentRateUpdateRateInfo() error {
	result, err := models.AgentGetRateById(r.AgentId)
	if err != nil {
		return err
	}

	if cast.ToFloat64(result.WechatMin) > r.WechatMin || cast.ToFloat64(result.WechatMax) < r.WechatMin {
		return errors.New("微信手续费超出范围")
	}
	if cast.ToFloat64(result.AlipayMin) > r.AlipayMin || cast.ToFloat64(result.AlipayMax) < r.AlipayMin {
		return errors.New("支付宝手续费超出范围")
	}
	if cast.ToFloat64(result.YunPayLowMin) > r.YunPayLowMin || cast.ToFloat64(result.YunPayLowMax) < r.YunPayLowMin {
		return errors.New("云闪付手续费超出范围")
	}
	if cast.ToFloat64(result.YunPayHighMin) > r.YunPayHighMin || cast.ToFloat64(result.YunPayHighMax) < r.YunPayHighMin {
		return errors.New("云闪付手续费超出范围")
	}
	if cast.ToFloat64(result.DebitCardMin) > r.DebitCardMin || cast.ToFloat64(result.DebitCardMax) < r.DebitCardMin {
		return errors.New("借记卡手续费超出范围")
	}
	if cast.ToFloat64(result.DebitCardCappingMin) > r.DebitCardCappingMin || cast.ToFloat64(result.DebitCardCappingMax) < r.DebitCardCappingMin {
		return errors.New("借记卡封顶额超出范围")
	}
	if cast.ToFloat64(result.CreditCardMin) > r.CreditCardMin || cast.ToFloat64(result.CreditCardMax) < r.CreditCardMin {
		return errors.New("信用卡手续费超出范围")
	}
	if cast.ToFloat64(result.D0SettlementRateMin) > r.D0SettlementRateMin || cast.ToFloat64(result.D0SettlementRateMax) < r.D0SettlementRateMin {
		return errors.New("d0结算费率超出范围")
	}
	if cast.ToFloat64(result.D0SettlementMin) > r.D0SettlementMin || cast.ToFloat64(result.D0SettlementMax) < r.D0SettlementMin {
		return errors.New("d0结算手续费超出范围")
	}
	if cast.ToFloat64(result.D1SettlementRateMin) > r.D1SettlementRateMin || cast.ToFloat64(result.D1SettlementRateMax) < r.D1SettlementRateMin {
		return errors.New("d1结算费率超出范围")
	}
	if cast.ToFloat64(result.D1SettlementMin) > r.D1SettlementMin || cast.ToFloat64(result.D1SettlementMax) < r.D1SettlementMin {
		return errors.New("d1结算手续费超出范围")
	}
	if cast.ToFloat64(result.WithdrawalRateMin) > r.WithdrawalRateMin || cast.ToFloat64(result.WithdrawalRateMax) < r.WithdrawalRateMin {
		return errors.New("提现费率超出范围")
	}
	if cast.ToFloat64(result.WithdrawalMin) > r.WithdrawalMin || cast.ToFloat64(result.WithdrawalMax) < r.WithdrawalMin {
		return errors.New("提现手续费超出范围")
	}

	//更新费率详情
	err = models.AgentRateUpdateRateInfo(map[string]interface{}{
		"wechat_min":             r.WechatMin,
		"alipay_min":             r.AlipayMin,
		"yun_pay_low_min":        r.YunPayLowMin,
		"yun_pay_high_min":       r.YunPayHighMin,
		"debit_card_min":         r.DebitCardMin,
		"debit_card_capping_min": r.DebitCardCappingMin,
		"credit_card_min":        r.CreditCardMin,
	}, r.RateId)
	if err != nil {
		return err
	}

	//更新结算信息
	err = models.AgentSettlementRateUpdateSettlementInfo(map[string]interface{}{
		"d0_settlement_rate_min": r.D0SettlementRateMin,
		"d0_settlement_min":      r.D0SettlementMin,
		"d1_settlement_rate_min": r.D1SettlementRateMin,
		"d1_settlement_min":      r.D1SettlementMin,
		"withdrawal_rate_min":    r.WithdrawalRateMin,
		"withdrawal_min":         r.WithdrawalMin,
	}, r.SettlementId)
	if err != nil {
		return err
	}

	return nil
}

// AgentRateGetRateInfo 获取费率信息
func (r *Rate) AgentRateGetRateInfo() (interface{}, error) {
	//获取费率详细
	rate, err := models.AgentRateGetRateInfo(r.AgentId)
	if err != nil {
		return nil, err
	}

	//获取结算信息
	settlement, err := models.AgentSettlementRateGetSettlementInfo(r.AgentId)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"rate":       rate,
		"settlement": settlement,
	}, nil
}
